// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let data = event.data
  data._openid = wxContext.OPENID
  data.time = new Date(data.time)
  let goods = await db.collection('lostGoods_list').add({
    data: data
  })

  data._id = goods._id
  await db.collection('gods').where({
    _openid: wxContext.OPENID,
  }).update({
    data: {
      'publish.lostGoods': _.push(data),
      btList: _.push({
        name: "发布失物招领信息",
        time: new Date(),
        num: 5
      })
    }
  })

  return data
}